package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.domain.CheckGroup;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.mapper.CheckGroupMapper;
import com.itheima.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = CheckGroupService.class)
@Transactional
public class CheckGroupServiceImpl implements CheckGroupService {

    @Autowired
    private CheckGroupMapper checkGroupMapper;

    //添加检查组
    public void add(CheckGroup checkGroup,Integer[] checkitemIds) {
        checkGroupMapper.add(checkGroup);
        setCheckGroupAndCheckItem(checkGroup.getId(),checkitemIds);
    }

    //分页查询
    public PageResult findPage(QueryPageBean queryPageBean) {
        //获取查询条件
        String queryString = queryPageBean.getQueryString();
        //获取
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        PageHelper.startPage(currentPage,pageSize);
        Page<CheckGroup> page = checkGroupMapper.selectByCondition(queryString);
        long total = page.getTotal();
        List<CheckGroup> result = page.getResult();
        return new PageResult(total, result);
    }

    //根据id查询
    public CheckGroup findById(Integer id) {
        return checkGroupMapper.findById(id);
    }

    //通过检查组id查询对应的检查项ids
    public List<Integer> findCheckItemIdsByCheckGroupId(Integer id) {
        List<Integer> list = checkGroupMapper.findCheckItemIdsByCheckGroupId(id);
        return list;
    }

    //根据id删除
    public void delete(Integer id) {
        //先删除关联关系
        checkGroupMapper.deleteAssociation(id);
        //删除检查组
        checkGroupMapper.delete(id);
    }

    //修改
    public void edit(CheckGroup checkGroup, Integer[] checkitemIds) {
        //1.修改检查组
        checkGroupMapper.edit(checkGroup);
        //2.删除原有的关联关系
        checkGroupMapper.deleteAssociation(checkGroup.getId());
        //3.重新添加修改后的关联关系
        setCheckGroupAndCheckItem(checkGroup.getId(),checkitemIds);
    }

    //查询所有
    public List<CheckGroup> findAll() {
        return checkGroupMapper.findAll();
    }

    //设置检查组合和检查项的关联关系
    public void setCheckGroupAndCheckItem(Integer checkGroupId,Integer[] checkitemIds){
        //判断checkitemIds是否为空
        if(checkitemIds != null && checkitemIds.length > 0){
            //不为空就进行遍历
            for (Integer checkitemId : checkitemIds) {
                //创建map容器,存储关联关系
                Map<String,Integer> map = new HashMap<>();
                map.put("checkgroupId",checkGroupId);
                map.put("checkitemId",checkitemId);
                //项中间表插入数据
                checkGroupMapper.setCheckGroupAndCheckItem(map);
            }
        }
    }
}
